package com.mark.basic.controller;

import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.lang.invoke.MutableCallSite;

@RequestMapping("/fastDfs")
@RestController
public class FastdfsController {
    // 文件上传 ①post ②复杂表单 MultipartFile
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file",required = true) MultipartFile file){
        try {
            // 获取文件的文件名.getOriginalFilename()
            String filename = file.getOriginalFilename();
            // 获取文件的后缀名  （不需要取到.所以+1）
            String suffix = filename.substring(filename.lastIndexOf(".") + 1);
            System.err.println(file.getBytes());
            String path = FastdfsUtil.upload(file.getBytes(), suffix);
            return AjaxResult.me().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("上传错误，请重新上传！！");
        }
    }
    // 文件删除
    @DeleteMapping
    public AjaxResult delete(@RequestParam(value = "path",required = true) String path){
        try {
            System.out.println(path); // /group1/M00/03/59/rBE3kWJCWxqACFIEAABwkkPEKAc51.webp
            String pathTmp = path.substring(1); // group1/M00/03/59/rBE3kWJCWxqACFIEAABwkkPEKAc51.webp
            // 获取groupName
            String groupName = pathTmp.substring(0, pathTmp.indexOf("/")); // group1
            // 获取路径fileName
            String fileName = pathTmp.substring(pathTmp.indexOf("/") + 1); // M00/03/59/rBE3kWJCWxqACFIEAABwkkPEKAc51.webp
           FastdfsUtil.delete(groupName,fileName);
           return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙，请稍后再试！！");
        }
    }

}